package com.cafe.action.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.cafe.model.Customer;
import com.cafe.model.Gender;
import com.cafe.model.Notice;
import com.cafe.model.Placard;
import com.cafe.model.ProductCategory;
import com.cafe.service.CustomerService;
import com.cafe.service.NoticeService;
import com.cafe.service.PlacardService;
import com.cafe.service.ProductCategoryService;
import com.malamute.core.action.BaseAction;
import com.malamute.core.constant.SystemConstant;

/**
 * 前端web显示加登录方法
 * 
 */
public class WebViewAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	// 需要显示菜单类别，菜单
	@Autowired(required = true)
	private ProductCategoryService productCategoryService;
	@Autowired(required = true)
	private CustomerService customerService;
	@Autowired(required = true)
	private PlacardService placardService;
	@Autowired(required = true)
	private NoticeService noticeService;

	// 用户对象
	private Customer customer;

	private List productCategories;// 菜单类型

	private List placards;// 所有海报

	private List products;// 第一组要显示的菜单

	private List notices;// 可用的公告

	private Notice notice;// 公告

	private ProductCategory productCategory;

	private String loginPage;// 登录时候跳转的页面

	private String goPage;// 在隐藏域中可以做跳转配置

	private Map<String, String> genderMap = new HashMap<String, String>();

	public String getGoPage() {
		return goPage;
	}

	public void setGoPage(String goPage) {
		this.goPage = goPage;
	}

	public void prepare() {
		this.loadWebViewData();
		this.loadNotices();
		if (session.get(SystemConstant.SES_CUSTOMER_LOGIN) != null) {
			String idStr = SystemConstant.getCustomerId((String) session
					.get(SystemConstant.SES_CUSTOMER_LOGIN));
			customer = customerService.getCustomerById(Integer.parseInt(idStr));
		}
	}

	public void prepareIndex() {
		loadPlacards();
		setLoginPage("index.jsp");
		setGoPage("index.jsp");
	}

	// 首页
	public String index() {
		loadWebViewData();
		return SUCCESS;
	}

	// 菜单类型数据
	private void loadWebViewData() {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(ProductCategory.class);
		productCategories = productCategoryService
				.listAllProductCategory(criteria);
		if (!productCategories.isEmpty()) {
			ProductCategory productCategory = (ProductCategory) productCategories
					.iterator().next();
			// products = new ArrayList(productCategory.getProducts());
		}
		// 广告，海报显示在首页
	}

	private void loadPlacards() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Placard.class);
		placards = placardService.listPlacards(criteria);
	}

	private void loadNotices() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Notice.class);
		Date now = new Date();
		criteria.add(Restrictions.gt("endTime", now));// >
		criteria.add(Restrictions.lt("dueTime", now));// <
		notices = noticeService.listAvailableNotice(criteria);
	}

	public void prepareViewProductByCategory() {
		setLoginPage("products.jsp");// 在商品页登录时候，登录跳转为商品页
		setGoPage("products.jsp");
	}

	public String viewProductByCategory() {
		if (productCategory != null) {
			productCategory = productCategoryService
					.getProductCategoryById(productCategory.getId());
			products = new ArrayList(productCategory.getProducts());
		}
		return SUCCESS;
	}

	public void prepareLogin(){
		loadPlacards();
	}
	
	// 登录
	public String login() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
		if (productCategory != null) {
			productCategory = productCategoryService
					.getProductCategoryById(productCategory.getId());
			products = new ArrayList(productCategory.getProducts());
		}
		if (customer == null) {
			return INPUT;
		}
		criteria.add(Restrictions.eq("username", customer.getUsername()));
		List list = customerService.findCustomersByUsername(criteria);
		Collection<String> errors = new ArrayList<String>();
		if (list != null && !list.isEmpty()) {
			Customer tmp = (Customer) list.iterator().next();
			if (customer.getPassword().equals(tmp.getPassword())) {
				// 登录成功
				session.put(SystemConstant.SES_CUSTOMER_LOGIN,
						SystemConstant.SES_PREFIX_CUSTOMER + tmp.getId());
				customer = tmp;
				return SUCCESS;
			} else {
				errors.add(getText("customer.password.wrong"));
				setActionErrors(errors);
				return SUCCESS;
			}
		} else {
			errors.add(getText("customer.username.notfound"));
			setActionErrors(errors);
			return SUCCESS;
		}
	}

	public void prepareLogout() {
		setGoPage("index.jsp");
	}

	public String logout() {
		session.put(SystemConstant.SES_CUSTOMER_LOGIN, null);
		return SUCCESS;
	}

	public String customerForm() {
		return SUCCESS;
	}

	public void prepareRegisterForm() {
		setGoPage("register.jsp");
	}

	public String registerForm() {
		return SUCCESS;
	}

	public void prepareRegister() {
		setGoPage("register.jsp");
	}

	// 注册用户
	public String register() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
		criteria.add(Restrictions.eq("username", customer.getUsername()));
		List list = customerService.findCustomersByUsername(criteria);
		Collection<String> errors = new ArrayList<String>();
		if (!list.isEmpty()) {
			errors.add("用户" + customer.getUsername() + "已存在!");
			setActionErrors(errors);
			return INPUT;
		} else {
			customerService.saveCustomer(customer);
			return SUCCESS;
		}
	}

	public void prepareViewNotice() {
		setGoPage("notice.jsp");
	}

	public String viewNotice() {
		if (notice != null) {
			notice = noticeService.getNoticeById(notice.getId());
		}
		return SUCCESS;
	}

	public List getProductCategories() {
		return productCategories;
	}

	public void setProductCategories(List productCategories) {
		this.productCategories = productCategories;
	}

	public List getProducts() {
		return products;
	}

	public void setProducts(List products) {
		this.products = products;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public ProductCategory getProductCategory() {
		return productCategory;
	}

	public void setProductCategory(ProductCategory productCategory) {
		this.productCategory = productCategory;
	}

	public String getLoginPage() {
		return loginPage;
	}

	public void setLoginPage(String loginPage) {
		this.loginPage = loginPage;
	}

	public Map<String, String> getGenderMap() {
		for (Gender g : Gender.values()) {
			genderMap.put(g.getKey(), g.getValue());
		}
		return genderMap;
	}

	public List getPlacards() {
		return placards;
	}

	public List getNotices() {
		return notices;
	}


	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

}
